package flc.ast.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.PairResultActivity;
import flc.ast.bean.StarsBean;
import flc.ast.databinding.FragmentPairBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PairFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PairFragment extends BaseNoModelFragment<FragmentPairBinding> {

    private List<StarsBean> listStar = new ArrayList<>();
    private Dialog myPairSelDialog;
    private WheelView wheelView1, wheelView2;
    private StarsBean maleBean, femaleBean;
    private int malePos, femalePos;

    @Override
    protected int onCreate() {
        return R.layout.fragment_pair;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvMaleName.setOnClickListener(this);
        mDataBinding.tvFemaleName.setOnClickListener(this);
        mDataBinding.ivStartPair.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvMaleName:
            case R.id.tvFemaleName:
                myPairSelDialog.show();
                break;
            //选择配对星座弹框
            case R.id.tvDialogPairSelCancel://取消
                myPairSelDialog.dismiss();
                break;
            case R.id.tvDialogPairSelRight://确定
                myPairSelDialog.dismiss();
                malePos = wheelView1.getCurrentItem();
                femalePos = wheelView2.getCurrentItem();
                SPUtil.putInt(mContext, AppConfig.MalePos, wheelView1.getCurrentItem());
                SPUtil.putInt(mContext, AppConfig.FemalePos, wheelView2.getCurrentItem());
                setData();
                break;
            case R.id.ivStartPair://开始配对
                PairResultActivity.maleBean = maleBean;
                PairResultActivity.femaleBean = femaleBean;
                startActivity(PairResultActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
        //设置选择的星座下标 第一次进入时默认为白羊座
        malePos = SPUtil.getInt(mContext, AppConfig.MalePos, 0);
        femalePos = SPUtil.getInt(mContext, AppConfig.FemalePos, 0);
        setData();
        //选择配对星座弹框
        pairSelDialog();
    }

    private void getData() {
        listStar.add(new StarsBean(getString(R.string.star_sign1), R.drawable.aimg_xz2_by, getString(R.string.birth_date1)));
        listStar.add(new StarsBean(getString(R.string.star_sign2), R.drawable.aimg_xz2_jn, getString(R.string.birth_date2)));
        listStar.add(new StarsBean(getString(R.string.star_sign3), R.drawable.aimg_xz2_sz2, getString(R.string.birth_date3)));
        listStar.add(new StarsBean(getString(R.string.star_sign4), R.drawable.aimg_xz2_jx, getString(R.string.birth_date4)));
        listStar.add(new StarsBean(getString(R.string.star_sign5), R.drawable.aimg_xz2_sz, getString(R.string.birth_date5)));
        listStar.add(new StarsBean(getString(R.string.star_sign6), R.drawable.aimg_xz2_cn, getString(R.string.birth_date6)));
        listStar.add(new StarsBean(getString(R.string.star_sign7), R.drawable.aimg_xz2_tc, getString(R.string.birth_date7)));
        listStar.add(new StarsBean(getString(R.string.star_sign8), R.drawable.aimg_xz2_tx, getString(R.string.birth_date8)));
        listStar.add(new StarsBean(getString(R.string.star_sign9), R.drawable.aimg_xz2_ss, getString(R.string.birth_date9)));
        listStar.add(new StarsBean(getString(R.string.star_sign10), R.drawable.aimg_xz2_mx, getString(R.string.birth_date10)));
        listStar.add(new StarsBean(getString(R.string.star_sign11), R.drawable.aimg_xz2_sp, getString(R.string.birth_date11)));
        listStar.add(new StarsBean(getString(R.string.star_sign12), R.drawable.aimg_xz2_sy, getString(R.string.birth_date12)));
    }

    //选择配对星座弹框
    private void pairSelDialog() {
        myPairSelDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_pair_sel, null);
        myPairSelDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myPairSelDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogPairSelCancel);
        TextView dialogRight = inflate.findViewById(R.id.tvDialogPairSelRight);
        wheelView1 = inflate.findViewById(R.id.wheel_view1);
        wheelView2 = inflate.findViewById(R.id.wheel_view2);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
        wheelView1.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView1.setTextColorCenter(Color.parseColor("#333333"));
        wheelView1.setLineSpacingMultiplier(2.5f);
        wheelView1.setDividerType(WheelView.DividerType.WRAP);
        wheelView1.setCurrentItem(0);
        wheelView1.setItemsVisibleCount(5);
        wheelView1.setCyclic(true);
        wheelView2.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView2.setTextColorCenter(Color.parseColor("#333333"));
        wheelView2.setLineSpacingMultiplier(2.5f);
        wheelView2.setDividerType(WheelView.DividerType.WRAP);
        wheelView2.setCurrentItem(0);
        wheelView2.setItemsVisibleCount(5);
        wheelView2.setCyclic(true);
        //设置滚轮数据
        List<String> listMale = new ArrayList<>();//男
        List<String> listFemale = new ArrayList<>();//女

        for (StarsBean starsBean : listStar) {
            listMale.add(starsBean.getName());
            listFemale.add(starsBean.getName());
        }

        WheelAdapter adapter1 = new ArrayWheelAdapter(listMale);
        wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listFemale);
        wheelView2.setAdapter(adapter2);
    }

    private void setData() {
        maleBean = listStar.get(malePos);
        femaleBean = listStar.get(femalePos);
        mDataBinding.ivMaleImg.setImageResource(maleBean.getImg());
        mDataBinding.tvMaleName.setText(maleBean.getName());
        mDataBinding.ivFemaleImg.setImageResource(femaleBean.getImg());
        mDataBinding.tvFemaleName.setText(femaleBean.getName());
    }

}